﻿
@{
    Layout = null;
}

<link href="~/libs/layui-v2.9.21/layui-v2.9.21/layui/css/layui.css" rel="stylesheet" />

<form class="layui-form" action="" style="width:80%;margin:0 auto;">
	<div class="layui-form-item">
		<label class="layui-form-label">入库单编号</label>
		<div class="layui-input-block">
			<input type="text" name="WarehousingCode" lay-verify="required" placeholder="请输入入库单编号" autocomplete="off" style="width:300px" class="layui-input">
			<input type="checkbox" lay-skin="switch" lay-filter="switchTest">自动生成
		</div>
	</div>

	<div class="layui-form-item">
		<label class="layui-form-label">入库单名称</label>
		<div class="layui-input-block">
			<input type="text" name="WarehousingName" lay-verify="required" placeholder="请输入入库单名称" autocomplete="off" class="layui-input">
		</div>
	</div>
	<div class="layui-form-item">
		<label class="layui-form-label">生产工单编号</label>
		<div class="layui-input-block">
			<input type="text" name="WorkOrderCode" lay-verify="required" placeholder="请输入生产工单编号" autocomplete="off" class="layui-input">
		</div>
	</div>
	<div class="layui-form-item">
		<label class="layui-form-label">产品编码</label>
		<div class="layui-input-block">
			<input type="text" name="ProductCode" lay-verify="required" placeholder="请输入产品编码" autocomplete="off" class="layui-input">
		</div>
	</div>
	<div class="layui-form-item">
		<label class="layui-form-label">入库日期</label>
		<div class="layui-input-inline layui-input-wrap">
			<input type="text" name="WarehousingDate" id="ID-laydate-demo" placeholder="yyyy-MM-dd" class="layui-input">
		</div>
	</div>
	<div class="layui-form-item">
		<label class="layui-form-label">单据状态</label>
		<div class="layui-input-block">
			<input type="radio" name="Dstatus" value="true" title="草稿" checked>
			<input type="radio" name="Dstatus" value="false" title="已完成">
		</div>
	</div>
	<div class="layui-form-item">
		<div class="layui-input-block">
			<button type="submit" class="layui-btn" lay-submit lay-filter="add">立即提交</button>
			<button type="reset" class="layui-btn layui-btn-primary">重置</button>
			<button type="button" class="layui-btn layui-btn-primary" onclick="closeWorkstation()">返回</button>
		</div>
	</div>
</form>

<script src="~/libs/jquery/jquery.js"></script>
<script src="~/libs/layui-v2.9.21/layui-v2.9.21/layui/layui.js"></script>

<script>

	//关闭弹框
	function closeWorkstation() {
		var index = parent.layer.getFrameIndex(window.name); //先得到当前的索引
		parent.layer.close(index); //再执行关闭
	}

	layui.use(['form', 'laydate', 'util'], function () {
		var form = layui.form;
		var layer = layui.layer;
		var laydate = layui.laydate;
		var util = layui.util;

		 // 渲染
		laydate.render({
			elem: '#ID-laydate-demo'
		});

		// 监听复选框变化，自动生成客户编码
		form.on('switch(switchTest)', function (data) {
			if (data.elem.checked) {
				// 生成随机字母+数字的编码并设置到CustomerCode输入框中
				var randomNum = Math.floor(Math.random() * 10) + '';
				for (var i = 0; i < 5; i++) {
					randomNum += Math.floor(Math.random() * 10);
				}
				randomNum = 'RK' + randomNum;
				console.log(randomNum);
				$('input[name="WarehousingCode"]').val(randomNum);

			}
		});

		// 提交事件
		form.on('submit(add)', function (data) {
			var field = data.field; // 获取表单字段值

			console.log(field);
			// 发送 AJAX 请求添加数据
			$.ajax({
				url: '/Warehousing/AddWarehousing', // 替换成你的后端API添加接口URL
				type: 'POST', // 使用POST方法提交数据
				data: field, // 发送表单字段数据
				dataType: 'json', // 期望从服务器返回的数据类型
				success: function (res) {
					if(res == -1)
					{
						layer.msg('编号已存在！请重新输入！', {
							time: 1000,
							icon: 5
						});
					}
					else if(res == -2)
					{
						layer.msg('名称已存在！请重新输入！', {
							time: 1000,
							icon: 5
						});
					}
					else if (res == 0) { // 假设后端返回的成功状态码为0
						layer.alert('添加成功', { title: '操作结果' }, function () {
							// 添加成功后的逻辑，例如关闭弹窗，刷新表格等
							layer.closeAll(); // 关闭所有弹窗
							// 这里可以刷新表格，重新加载数据等
							closeWorkstation()
						});
					} else {
						layer.alert('添加失败：' + res.msg, { title: '操作结果' });
					}
				},
				error: function (err) {
					// 处理添加失败的情况
					layer.alert('添加失败：网络异常，请稍后重试', { title: '操作结果' });
					console.error('添加失败', err);
				}
			});

			return false; // 阻止默认 form 提交
		});
	});
</script>